package com.qfmy.mystation.utils;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Build;

import androidx.annotation.RequiresApi;

import java.io.IOException;

public class SetWallpaperUtil {
    public static final int FLAG_LOCK = 2;  // 设置锁屏壁纸
    public static final int FLAG_SYSTEM = 1;  // 设置桌面壁纸

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void setWallpaper(Context context, Bitmap bitmap, int type) {
        WallpaperManager mWallManager = WallpaperManager.getInstance(context);
        try {
            int width = mWallManager.getDesiredMinimumWidth();
            int height = mWallManager.getDesiredMinimumHeight();
            mWallManager.suggestDesiredDimensions(width, height);
            mWallManager.setBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), false, type);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
